package com.tarena.lbs.article.web.service;

import com.tarena.lbs.article.web.event.output.TriggerMsgOutputSource;
import com.tarena.lbs.article.web.repository.ActionESRepository;
import com.tarena.lbs.article.web.repository.ArticleESRepository;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.content.enums.ArticleActionEnum;
import com.tarena.lbs.common.content.enums.ContentResultEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.content.entity.ActionSearchEntity;
import com.tarena.lbs.pojo.content.entity.ArticleSearchEntity;
import com.tarena.lbs.pojo.content.param.ArticleActionParam;
import java.util.Date;
import java.util.UUID;

import com.tarena.lbs.pojo.message.event.TriggerMsgEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ActionService {
    @Autowired
    private ActionESRepository actionESRepository;
    @Autowired
    private ArticleESRepository articleESRepository;
    @Autowired
    private TriggerMsgOutputSource triggerMsgOutputSource;
    public void saveAction(ArticleActionParam param) throws BusinessException {
        ActionSearchEntity entityParam=new ActionSearchEntity();
        //1. 获取userId
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException(ContentResultEnum.AUTHENTICATION_DATA_ERROR));
        //action behavior comment
        BeanUtils.copyProperties(param,entityParam);
        entityParam.setId(UUID.randomUUID().toString());
        //2. 封装数据
        ArticleSearchEntity articleEntity = articleESRepository.findById(Integer.valueOf(param.getId())).get();
        entityParam.setArticleLabel(articleEntity.getArticleLabel());
        entityParam.setArticleTitle(articleEntity.getArticleTitle());
        entityParam.setArticleType(articleEntity.getArticleCategoryId()+"");
        entityParam.setArticleUserId(articleEntity.getUserId());
        entityParam.setBehaviorUserId(userPrinciple.getId());
        entityParam.setCreateTime(new Date());
        entityParam.setArticleId(param.getId());
        //3.写入
        actionESRepository.save(entityParam);
        //4.更新文章
        ArticleSearchEntity entityUpdateParam=new ArticleSearchEntity();
        BeanUtils.copyProperties(articleEntity,entityUpdateParam);
        entityUpdateParam.setId(Integer.valueOf(param.getId()));
        if (param.getAction().equals(1)){
            entityUpdateParam.setLikeCount(articleEntity.getLikeCount()+1);
        }else if (param.getAction().equals(2)){
            entityUpdateParam.setFavoriteCount(articleEntity.getFavoriteCount()+1);
        }else {
            entityUpdateParam.setAccessCount(articleEntity.getAccessCount()+1);
        }
        articleESRepository.save(entityUpdateParam);
        TriggerMsgEvent event=new TriggerMsgEvent();
        event.setUserId(userPrinciple.getId());
        event.setUsername(userPrinciple.getNickname());
        event.setArticleTitle(articleEntity.getArticleTitle());
        event.setArticleId(Integer.valueOf(param.getId()));
        event.setUserAction(param.getAction());
        event.setActionName(ArticleActionEnum.getNameByCode(param.getAction()));
        event.setActivityId(articleEntity.getActivityId());
        event.setAriticleUserId(articleEntity.getUserId());
        event.setArticleUsername(articleEntity.getArticleUsername());
        event.setSource(articleEntity.getSource());
        log.info("写入的消息:{}",event);
        MessageChannel messageChannel = triggerMsgOutputSource.triggerMsgOutput();
        Message<TriggerMsgEvent> message = MessageBuilder.withPayload(event).build();
        messageChannel.send(message);
    }
}
